home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C101.ZIP / UUPC11XT.ZIP / UTIL / FMT.C < prev    next >
C/C++ Source or Header  |  1992-11-14  |  5KB  |  155 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    Program:    fmt.c          14 August 1990                       */
  3. /*    Author:     Andrew H. Derbyshire                                */
  4. /*                108 Decatur St, Apt 9                               */
  5. /*                Arlington, MA 02174                                 */
  6. /*    Function:   Format lines into user specified width rows         */
  7. /*    Arguments:  width, input file, output file.                     */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. /*--------------------------------------------------------------------*/
  11. /*                          RCS Information                           */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. /*
  15.  *    $Header: E:\SRC\UUPC\UTIL\RCS\FMT.C 1.1 1992/11/15 04:29:22 ahd Exp $
  16.  *
  17.  *    Revision history:
  18.  *    $Log: FMT.C $
  19.  * Revision 1.1  1992/11/15  04:29:22  ahd
  20.  * Initial revision
  21.  *
  22.  * Revision 1.2  1992/04/27  00:41:11  ahd
  23.  * Add RCS Information
  24.  *
  25.  */
  26.  
  27. static char rscid[] = "$Id: FMT.C 1.1 1992/11/15 04:29:22 ahd Exp $";
  28.  
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33.  
  34. #include "lib.h"
  35. #include "timestmp.h"
  36.  
  37. /*--------------------------------------------------------------------*/
  38. /*    m a i n                                                         */
  39. /*                                                                    */
  40. /*    main program                                                    */
  41. /*--------------------------------------------------------------------*/
  42.  
  43.  void main( int argc, char *argv[] )
  44.  {
  45.    int width = 0;          /* Width of current line                  */
  46.    int maxwidth = 72;      /* Max width of line allowed              */
  47.    char buf[BUFSIZ];       /* Our I/O buffer                         */
  48.    int punct = 0;          /* Last character in last word was punct  */
  49.    int argx = 1;           /* Current argument being processed       */
  50.    FILE *input;
  51.    FILE *output;
  52.  
  53. /*--------------------------------------------------------------------*/
  54. /*                        Announce our version                        */
  55. /*--------------------------------------------------------------------*/
  56.  
  57.    banner( argv );
  58.  
  59. /*--------------------------------------------------------------------*/
  60. /*                            Handle help                             */
  61. /*--------------------------------------------------------------------*/
  62.  
  63.    if (( argc > 1 ) && equal(argv[1],"-?"))
  64.    {
  65.       printf("Usage:\tfmt\t[-#] infile outfile\n");
  66.       exit(1);
  67.    }
  68.  
  69. /*--------------------------------------------------------------------*/
  70. /*       Get the line width if the user specified it                  */
  71. /*--------------------------------------------------------------------*/
  72.  
  73.     if ((argx < argc) && (*argv[argx] == '-'))
  74.       maxwidth = atoi( argv[argx++] );
  75.  
  76. /*--------------------------------------------------------------------*/
  77. /*                  Get the input file name, if any                   */
  78. /*--------------------------------------------------------------------*/
  79.  
  80.     if (argx == argc)
  81.       input = stdin;
  82.     else {
  83.       input = fopen(argv[argx++],"r");
  84.       if (input == NULL)
  85.       {
  86.          perror(argv[--argx]);
  87.          exit (100);
  88.       }
  89.     }
  90.  
  91. /*--------------------------------------------------------------------*/
  92. /*                  Get the output file name, if any                  */
  93. /*--------------------------------------------------------------------*/
  94.  
  95.     if (argx == argc )
  96.       output = stdout;
  97.     else {
  98.       output = fopen(argv[argx++],"w");
  99.       if (output == NULL)
  100.       {
  101.          perror(argv[--argx]);
  102.          exit( 200 );
  103.       }
  104.     }
  105.  
  106. /*--------------------------------------------------------------------*/
  107. /*                          Process the file                          */
  108. /*--------------------------------------------------------------------*/
  109.  
  110.     while( fgets(buf, BUFSIZ, input) != NULL )
  111.     {
  112.       char *token = strtok(buf, " \t\n");
  113.       if (token == NULL )
  114.       {
  115.          fputc('\n',output);
  116.          width = punct = 0;
  117.       }
  118.       else while(token != NULL)
  119.       {
  120.          register size_t toklen = strlen(token);
  121.          width = toklen + width + 1 + punct;
  122.          if (width > max(maxwidth, toklen + 1))
  123.          {
  124.             fputc('\n',output);
  125.             width = toklen;
  126.             punct = 0;
  127.          }
  128.          else {
  129.             if (width > (toklen + 1))
  130.             {
  131.                fputc(' ',output);
  132.                if (punct)
  133.                   fputc(' ',output);
  134.             }
  135.             else
  136.                width = toklen;
  137.             punct = ispunct( token[toklen - 1] ) ? 1 : 0;
  138.          } /* else */
  139.          fputs(token, output);
  140.          token = strtok(NULL, " \t\n");
  141.       } /* else while */
  142.    } /* while */
  143.  
  144.    if (ferror(input))
  145.    {
  146.       perror(argv[1]);
  147.       clearerr(input);
  148.    }
  149.  
  150.    fclose(input);
  151.    fclose(output);
  152.  
  153.    exit (0);
  154.  } /* main */
  155.